<template>
  <div class="container">
    <!-- search -->
    <van-search
      v-model="value"
      show-action
      placeholder="半岛铁盒"
      shape="round"
      input-align="center"
      disabled
      @click="goSearch"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>

    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item
        v-for="(item, index) in banner"
        :key="index"
        @click="goDetail(item.encodeId)"
      >
        <img :src="item.imageUrl" alt="" width="100%" />
      </van-swipe-item>
    </van-swipe>

    <!-- 标题 -->
    <!-- <ul class="tagContainer">
        <li class="tagItem" v-for="item in iconArr" :key="item.id">
            <img :src="item.iconUrl" alt="" width="100%">
            <span>{{item.name}}</span>
        </li>
    </ul> -->
    <div class="navbar">
      <van-tabs v-model="active">
        <van-tab v-for="item in iconArr" :key="item.id">
          <template #title>
            <div class="imgContainer">
              <img :src="item.iconUrl" alt="" width="100%" />
            </div>
            <span class="txt">{{ item.name }}</span>
          </template>
        </van-tab>
      </van-tabs>
    </div>
    <!-- 分隔线 -->
    <van-divider />
    <!-- 推荐歌单 -->
    <div class="recommend">
      <!-- 头部 -->
      <div class="header">
        <h3>推荐歌单</h3>
        <div class="more">更多></div>
      </div>
      <!-- 主体 -->
      <div class="main">
        <van-tabs v-model="recommendState">
          <van-tab v-for="obj in recommendList" :key="obj.id">
            <template #title>
              <div>
                <div class="show" @click="playListDetail(obj.id)">
                  <img :src="obj.picUrl" alt="" />
                  <div class="count icon-24gl-play iconfont">
                    {{ obj.trackCount }}万
                  </div>
                </div>
                <p class="van-multi-ellipsis--l2">{{ obj.name }}</p>
              </div>
            </template>
          </van-tab>
        </van-tabs>
      </div>
    </div>
    <!-- 分隔线 -->
    <van-divider />
    <!-- 热门话题 -->
    <div class="hot-talk">
      <div class="header">
        <h4>热门话题</h4>
        <div>
          <van-icon name="replay" @click="freshHotTalkShowListFn" />
        </div>
      </div>
      <van-cell
        v-for="obj in hotTalkShowList"
        :key="obj.artId"
        icon="location-o"
      >
        <template #icon>
          <van-icon name="guide-o" color="blue" />
        </template>
        <template #title>
          <span style="font-size: 13px">{{ obj.title }}</span>
        </template>
        <template #default>
          <span style="font-size: 12px">{{
            `${obj.participateCount}万热度`
          }}</span>
        </template>
      </van-cell>
    </div>
  </div>
</template>

<script>
import {
  getBannerData,
  getIcon,
  getRecommendApi,
  getHotTalkApi,
  getPlayListDetail
} from "@/request/api";
export default {
  name: "HomeVue",
  data() {
    return {
      // 搜索框value
      value: "",
      // 轮播图数据
      banner: [],
      iconArr: [],
      active: 1,
      recommendState: 1, // 推荐歌单选中状态
      recommendList: [], // 推荐歌单列表
      hotTalkList: [], //热门话题列表
      hotTalkShowList: [],
      indexHot: 3,
    };
  },
  methods: {
    // 请求首页数据
    async getBannerData() {
      const { data: res } = await getBannerData();
      if (res.code !== 200) {
        this.$toast("请求失败");
      }
      // 请求成功
      this.banner = res.banners;
    },
    //请求icon
    async getIcon() {
      const { data: res } = await getIcon();
      if (res.code !== 200) {
        this.$toast("请求失败");
      }
      // 请求成功
      this.iconArr = res.data;
      console.log(res);
    },
    // search事件
    onSearch() {},
    // 获取推荐歌单
    async getrecommendList() {
      const res = await getRecommendApi();
      this.recommendList = res.data.recommend;
      console.log("commend", res);
    },
    // 获取热门话题
    async getHotTalkApi() {
      const res = await getHotTalkApi();
      console.log("hotTalk", res);
      // 处理
      this.hotTalkList = res.data.hot.map((item) => {
        item.participateCount = Math.ceil(item.participateCount / 100);
        return item;
      });
      this.hotTalkShowList = this.hotTalkList.slice(0, this.indexHot);
    },
    // 刷新热门话题展示列表
    freshHotTalkShowListFn() {
      if (this.indexHot >= 19) this.indexHot = 0;
      this.hotTalkShowList = this.hotTalkList.slice(
        this.indexHot,
        this.indexHot + 3
      );
      this.indexHot = this.indexHot + 3;
    },
    goDetail(id) {
      this.$router.push({
        name: "SongDetail",
        query: {
          id,
        },
      });
    },
    goSearch(){
      this.$router.push('/search')
    },
    async playListDetail(id) {
      // console.log(id);
      try {
        const { data: res } = await getPlayListDetail({ id });
        console.log(res);
        if (res.code !== 200) return this.$toast("获取歌单详情失败");
        this.$store.commit("GETMUSICID", res.playlist.trackIds);
        this.$router.push({
          name: "Playdetail",
          query: {
            name: res.playlist.name,
          },
        });
      } catch (error) {
        console.log(error.response.data.message);
      }
    },
  },
  created() {
    this.getBannerData();
    this.getIcon();
    this.getrecommendList();
    this.getHotTalkApi();
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 0 3%;
}
.tagContainer {
  display: flex;
  height: 80px;
  background-color: antiquewhite;
  .tagItem {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    img {
      width: 100%;
      height: 100%;
      background-color: red;
    }
  }
}
/deep/ .van-tabs__line {
  display: none;
}
.imgContainer {
  text-align: center;
  img {
    border-radius: 50%;
    width: 38px;
    // height: 50px;

    background-color: red;

    // filter:drop-shadow( 0px 0 0px red);
  }
}
/deep/ .van-tabs--line .van-tabs__wrap {
  height: 100%;
}
/deep/ .van-tabs__nav {
  .van-tab {
    &:first-child {
      padding-left: 0;
    }
  }
}
/deep/ .van-tab__text {
  text-align: center;
}
.navbar {
  .van-tabs {
    margin-top: 10px;
  }
  .txt {
    font-size: 12px;
    color: #000;
  }
}
.recommend {
  .header {
    display: flex;
    justify-content: space-between;
    h3 {
      font-size: 16px;
      letter-spacing: 1px;
    }
    .more {
      padding: 0 8px;
      font-size: 12px;
      border: 1px solid #bbb;
      border-radius: 15px;
      letter-spacing: 1px;
    }
  }
  .main {
    margin-top: 10px;
    /deep/ .van-tab {
      padding: 0;
      margin-right: 8px;
    }
    .show {
      position: relative;
      width: 105px;
      height: 105px;
      img {
        width: 100%;
        border-radius: 5px;
      }
      .count {
        position: absolute;
        top: 3px;
        right: 2px;
        padding: 0 8px;
        background-color: rgba(222, 222, 222, 0.5);
        color: #fff;
        font-size: 12px;
        border-radius: 15px;
        transform: scale(0.83);
      }
    }
    p {
      width: 105px;
      height: 40px;
      text-align: left;
      color: #000;
      font-size: 12px;
      word-wrap: break-word;
      word-break: break-all;
      overflow: hidden;
    }
  }
}

.my-swipe {
  border-radius: 5px;
}
.hot-talk {
  padding-bottom: 50px;
  .header {
    display: flex;
    justify-content: space-between;
  }
  .van-cell {
    padding-left: 0;
    line-height: 100%;
  }
  .van-icon {
    margin-right: 5px;
  }
  h4 {
    margin-bottom: 12px;
  }
}
</style>